function Tree(objClass, eId, eName, module, controller){
	this.objClass = objClass;
	this.urlPath = baseUrl + '/' + module + '/' + controller;
	this.eId = eId;
	this.eName = eName;
	this.loadTree();
}

Tree.prototype.loadTree=function (){
	var self = this;
	var url = this.urlPath + '/loadtree';;
	var obj = $("#jsTree");
			obj.jstree({
			"plugins" : [ "themes", "json_data", "ui","contextmenu","hotkeys","search","types","crrm" ],
			"json_data" : { 
				"ajax" : {
					"url" : url,
					"data" : function (n) { 
						return { 
							"operation" : "get_children", 
							"id" : n.attr ? n.attr("id").replace("node_","") : '',
						}; 
					}
				}
			}
			,"types" : {
				"max_depth" : -2,
				"max_children" : -2,
				"valid_children" : [ "drive" ],
				"types" : {
					"user" : {
						"icon" : {
							"image" : "../../../public/images/Users.png"
						}
					},
				},
				"default" : {
					"valid_children" : [ "default" ]
				}
			}
			 ,"search" : {
	            "ajax" : {
	                "url" : url,
	                "data" : function (str) {
	                    return {
	                        "operation" : "search",
	                        "search_str" : str
	                    };
	                }
	            }
	        }
			,"themes" : {
	            "theme" : "default",
	            "dots" : false,
	            "icons" : true,
	            "url": baseUrl + '/public/lib-js/system/jsTree/themes/default/style.css'
	    		}
			,contextmenu: {items: customMenu}
			,callback : {
		        onrgtclk    : function() { alert(12); },
		      }
		})

		// .bind("hover_node.jstree", function (e, data) {
		//     $('#idNode').val(data.rslt.obj.attr("id"));
  //           $('#nameNode').val(data.inst.get_text(data.rslt.obj));
		// })
		.bind("select_node.jstree", function (event, data) {
	            obj.jstree("toggle_node", data.rslt.obj);
	            $('#idNode').val(data.rslt.obj.attr("id"));
	            $('#nameNode').val(data.inst.get_text(data.rslt.obj));
			})
		.delegate("a.jstree-clicked", "click", function (event, data) {
			// console.log(data)
			// alert(data.rslt.obj.attr("id"));
				event.preventDefault();
			})
		.delegate("a","dblclick", function(e) {

		});
		var to = false;
		  $('#searchNodes').keyup(function () {
		    if(to) { clearTimeout(to); }
		    to = setTimeout(function () {
		      var v = $('#searchNodes').val();
		      // $("#jsTree").jstree(true).search(v);
		    }, 250);
		  });

	}

function customMenu(node) {
	// $("#jsTree")
	var nodeid = $(node).attr('id')
		,nodeName = $(node).children('a:first').text()
		,parentID = $(node).parent().parent().attr('id');
	var objTree = $("#jsTree");
	// obj.jstree("select_node", node).trigger("select_node.jstree");
	var items = {};
	if ($(node).hasClass("unit") || $(node).hasClass("home"))
		items = {
			"add" : {
					"separator_before"	: true,
					"separator_after"	: true,
					"icon"				: false,
					"label"             : "<i></i><span class='vakata-contextmenu-sep'>&nbsp;</span>Thêm mới",
					"action"			: false,
					"submenu" : { 
						"unit" : {
							"separator_before"	: true,
							"separator_after"	: true,
							// "label"				: "Phòng ban",
							"label"             : "<i></i><span class='vakata-contextmenu-sep'>&nbsp;</span>Phòng ban",
							"action"			: function () { myUser.addUnit(nodeid,nodeName);}
						},
						"staff" : {
							"separator_before"	: true,
							"separator_after"	: true,
							"icon"				: false,
							"label"             : "<i></i><span class='vakata-contextmenu-sep'>&nbsp;</span>Cán bộ",
							"action"			: function () { myUser.addStaff(nodeid,nodeName); }
						},
					}
				},
			"edit" : {
					"separator_before"	: true,
					"separator_after"	: false,
					"label"             : "<i></i><span class='vakata-contextmenu-sep'>&nbsp;</span>Sửa phòng ban",
					"action"			: function (obj) {
													// $(node).trigger('click');
													// $("#jsTree").jstree("select_node", node).trigger("select_node.jstree");
												 	myUser.edit(nodeid,nodeName);
												}
				},
			"Del" : {
					"separator_before"	: true,
					"separator_after"	: false,
					"label"             : "<i></i><span class='vakata-contextmenu-sep'>&nbsp;</span>Xóa phòng ban",
					"action"			: function (obj) {
													var checked_ids = [];
													objTree.jstree('get_selected').each(function(){
														if ($(this).hasClass("unit")) {
													      	checked_ids.push($(this).attr('id'));
														};
													});
													if(checked_ids.length == 0) checked_ids.push(nodeid);
													myUser.delete(checked_ids.join(",")); 
												}
				},
		};
	if ($(node).hasClass("staff"))
		items = {
			"edit" : {
					"separator_before"	: true,
					"separator_after"	: true,
					"label"             : "<i></i><span class='vakata-contextmenu-sep'>&nbsp;</span>Sửa cán bộ",
					"action"			: function (obj) { 
												$(node).trigger('click');
												myUser.editstaff(nodeid,nodeName); 
											}
				},
			"Del" : {
					"separator_before"	: true,
					"separator_after"	: true,
					"label"             : "<i></i><span class='vakata-contextmenu-sep'>&nbsp;</span>Xóa cán bộ",
					"action"			: function (obj) { 
												var checked_ids = [];
												objTree.jstree('get_selected').each(function(){
													if ($(this).hasClass("staff")) {
												      	checked_ids.push($(this).attr('id'));
													};
												});
												if(checked_ids.length == 0) checked_ids.push(nodeid);
												myUser.deletestaff(checked_ids.join(","));
											}
				},
		};

		items.Refresh = {
                    "separator_before"  : true,
                    "separator_after"   : true,
                    "label"             : "<i></i><span class='vakata-contextmenu-sep'>&nbsp;</span>Refresh",
                    "action"            : function (obj) { 
                        $('#refreshTree').trigger('click');  
                        // self.refresh();
                    }
                }
	// if ($(node).hasClass("folder")) {
	// 	// Delete the "delete" menu item
	// 	delete items.deleteItem;
	// }
	return items;
}
